{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "# 元组允许在一条语句中完成对所有变的赋值。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "1. 将元组赋给单独声明的变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "(string 国家,string 首都,double 人均GDP)=(\"南苏丹\",\"朱巴\",275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家},首都{首都}:{人均GDP}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "2. 将元组赋给预声明的变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//将元组赋给预声明的变量\n",
    "string 国家;\n",
    "string 首都;\n",
    "double 人均GDP;\n",
    "(国家,首都,人均GDP)=(\"南苏丹\",\"朱巴\",275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家},首都{首都}:{人均GDP}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "3. 将元组赋给单独声明和隐式类型的变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "(var 国家,var 首都,var 人均GDP)=(\"南苏丹\",\"朱巴\",275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家},首都{首都}:{人均GDP}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "4. 将元组赋给单独声明和隐匿类型的变量，便只用了一个var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "var ( 国家, 首都, 人均GDP)=(\"南苏丹\",\"朱巴\",275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家},首都{首都}:{人均GDP}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "5. 声明具名元组，将元组值赋给它，按名称访问元组项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "(string 国家,string 首都,double 人均GDP) 国家信息=(\"南苏丹\",\"朱巴\",275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家信息.国家},首都{国家信息.首都}:{国家信息.人均GDP}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "6. 声明包含具名元组项的元组，将其赋给隐工类型的变量，按名称访问元组项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "var 国家信息= (Name:\"南苏丹\",Capital:\"朱巴\",GDP:275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家信息.Name},首都{国家信息.Capital}:{国家信息.GDP}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "7. 声明包含具名元组项的元组，将其赋给隐式类型的变量，按名称访问元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "var 国家信息= (\"南苏丹\",\"朱巴\",275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家信息.Item1},首都{国家信息.Item2}:{国家信息.Item3}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "8. 将元组项具名的元组赋给隐式类型的变量，但还是通过项编号属性访问单独的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "var 国家信息= (Name:\"南苏丹\",Capital:\"朱巴\",GDP:275.18);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家信息.Item1},首都{国家信息.Item2}:{国家信息.Item3}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "9. 赋值时用下划组丢弃元组的一部分（弃元）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "using System;\n",
    "(string name, _,double Gdp) =(\"南苏丹\",\"朱巴\",275.18);\n",
    "\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{name},:{Gdp}\");\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    " 10. 通过变量和属性名推断元组项名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "string 国家= \"南苏丹\";\n",
    "string 首都=\"朱巴\";\n",
    "double gdp=275.18;\n",
    "var 国家信息= (国家,首都,gdp);\n",
    "System.Console.WriteLine($@\"2019年世界上最贫穷的国家是{国家信息.Item1},首都{国家信息.Item2}:{国家信息.Item3}\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "var(fi,se)=System.ValueTuple.Create<string,string>(\"\",\"\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "**3.5数组**"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "前向访问数组  \n",
    "&amp;  \n",
    "& &amp;  \n",
    "AT&T  \n",
    "> 反向访问数组\n",
    ">> 从C#8.0开始,还可以从末尾索引数组,例如,adm ^1对应于为数组的最后一个元素编制,^3对应于为倒数第三个元素编制索引\n",
    ">> 反向访问数据索引从倒数1开始\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//表3.2 数组的重点\n",
    "string[] languages=new string[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"};\n",
    "\n",
    "string language=languages[4];\n",
    "System.Console.WriteLine(language);\n",
    "language=languages[^3];\n",
    "System.Console.WriteLine(language);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "**区间**\n",
    "> C#8.0允许使用范围运算符标识和提取元素数组,该运算符包括起始项,但不包括结束项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//表3.2 数组的重点\n",
    "System.Console.WriteLine($@\"^3..^0:{string.Join(\",\",languages[^3..^0])}\");\n",
    "System.Console.WriteLine($@\"^3..:{string.Join(\",\",languages[^3..])}\");\n",
    "System.Console.WriteLine($@\"3..^3:{string.Join(\",\",languages[3..^3])}\");\n",
    "System.Console.WriteLine($@\"..6:{string.Join(\",\",languages[..^6])}\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "#### 3.5.1 数组的声明\n",
    "只有在同一条语句中声明并赋值,才能使用代码清单3.9的赋值语法,声明后基其他地方赋值则需使用new关键字,如代码清单3.10所示."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//代码清单3.9 声明数组的同时赋值\n",
    "string[] languages=new string[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//代码清单3.10  声明数组后赋值\n",
    "string [] languages;\n",
    "languages=new strirng[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"};\n",
    "languages=new[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"}; //C#3.0后\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//代码清单3.11 声明数组时用new赋值\n",
    " string [] languages=new[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"}; "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.12  声明数组时用new关键字赋值并指定数组大小\n",
    " string [] languages=new string[9]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"}; //new string[]中,string不能省略\n",
    " System.Console.WriteLine(string.Join(\",\\n\\r\",languages));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.13 分配数据但不提供初始值\n",
    "string[] languages=new string[9];"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "> 分配数组便不指定初始值,\"运行时\"将每个数组元素初始化为它们的默认值,如下所示:\n",
    ">\n",
    "- 引用类型,不论是否为空,都初始化为null  \n",
    "- 可空的什类型初始化为null.  \n",
    "- 不可空的值类型初始化为0.  \n",
    "- bool初始化为false.  \n",
    "- char初始化为\\0.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.14 在运行时确定数组大小\n",
    "string[] groceryList;\n",
    "System.Console.WriteLine(\"how many items on the list?\");\n",
    "int size=int.parse(Console.ReadLine());\n",
    "groceryList=new string [size];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.15声明二维数组\n",
    "int[,]cells=new int[3,3];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "int[,] cells={\n",
    "    {1,0,2},\n",
    "    {1,2,0},\n",
    "    {1,2,1}\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "bool [,,] cells;\n",
    "cells=new bool[2,3,3]\n",
    "{\n",
    "    {\n",
    "        {true,false,false},\n",
    "        {true,false,false},\n",
    "        {true,false,true}\n",
    "    },\n",
    "    {\n",
    "        {false,false,true},\n",
    "        {false,true,false},\n",
    "        {false,true,false}\n",
    "    }\n",
    "};"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "**上面这种总不能多维数组也称为\"矩形数组\",第一组的大小必段一致,还可以定交交错数组(jagged array),也就是由数组构成的数组.不需要具有一致的大小.**\n",
    "    - 交错数组定义由数组构成的数组."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "int[][] cells={\n",
    "    new int[]{1,0,2,0},\n",
    "    new int[]{1,2,0},\n",
    "    new int[]{1,2},\n",
    "    new int[]{1}        \n",
    "};\n",
    "int [][] cellss=new int[2][4];"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "##### 3.5.3 数组的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    " string [] languages=new[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"}; \n",
    "System.Console.WriteLine(languages);\n",
    " System.Console.WriteLine(string.Join(\",\\n\",languages[..6]));"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "- Length返回数组中元素的总数.因此,如果你有一个多维数组,比如大小为2*3*的bool cells[,,]数组,,那length会返回元素总数18  .\n",
    "- 对为于交错数组,length返回外部数组的元素数.因数交错数组是\"数组构成的数组\",所以Length只作用于外部数组,只统计它了的元素数,也就是具体由多少个数组构成,而不管各内部数组共包含了多少个元素.  \n",
    "**区间**\n",
    "C#8.0为数组提供了一个新的访问:数组切片.简单地说,数组切片就是将原数组中特定长度的一段连续元素提取出来形成背后数组.  \n",
    "我们将数组中一段连续的远程称为区间.  \n",
    "区间操作符..表示.  \n",
    "区间操作所代表的区间为半闭半开区间.  \n",
    "写在它左侧的索引所代表的元素被包含在区间内,而写在右侧的索引所代表的元素则不被包含.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    " string [] languages=new[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"}; \n",
    " System.Index index=^1;\n",
    "\n",
    " \n",
    " System.Console.WriteLine(languages[index]);\n",
    "System.Range range=..^1;\n",
    "System.Console.WriteLine(string.Join(\",\",languages[range]));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.28 更多数组方法\n",
    "\n",
    " string[] languages=new string[]{\"C#\",\"COBOL\",\"Java\",\"C++\",\"TypeScript\",\"VB\",\"python\",\"Lisp\",\"JavaScript\"}; \n",
    "\n",
    " // System.Array.Sort(languages); //对数组小的元素排序\n",
    " System.Console.WriteLine(string.Join(\",\",languages));\n",
    " string searchString=\"COBOL\";\n",
    " int index=System.Array.BinarySearch(languages,searchString);\n",
    " System.Console.WriteLine();\n",
    " System.Console.WriteLine($\"\\\"{searchString}\\\"is at index:{index}\");//搜索元素在数组中的索引\n",
    " System.Console.WriteLine($\"{\"first Element\",-20}\\t{\"Last Element\",-20}\"); //正数20表示占位20,不足从前面补空格,-20则从后面补空格,\\t占一个Tab位\n",
    " System.Console.WriteLine($\"{\"------------\",-20}\\t{\"------------\",-20}\");\n",
    " System.Console.WriteLine($\"{languages[0],-20}\\t{languages[^1],-20}\");\n",
    " System.Array.Reverse(languages);//反转一维数组中元素的顺序.\n",
    " System.Console.WriteLine($\"{languages[0],-20}\\t{languages[^1],-20}\");\n",
    " //Note this does ont remove all items from the array.\n",
    " //Rather,it sets each item to the type's default value.\n",
    " //注意这不会从数组中移除所有的项。\n",
    " //相反，它将每个项设置为类型的默认值。\n",
    " System.Array.Clear(languages,0,languages.Length);\n",
    " System.Console.WriteLine($\"{languages[0],-20}\\t{languages[^1],-20}\"); //空行\n",
    " System.Console.WriteLine($\"清除后，数组大小为:{languages.Length}\");\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "- 这些方法通过System.Array类提供,大多数都一目了然,但注意以下两点:  \n",
    "    -使用BinarySearch()方法前要先对数组进行排序,否则会回不正确的索引.  \n",
    "    -Clear()方法不删除数组元素,不将长度设为零.数组大小固定,不能修改.所以Clear()方法将每个元素设为其默认值(null,0或false).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//允许改变数组大小\n",
    "\n",
    "//创建并初始化一个校招字符串数组\n",
    "string[] myArr={\"The\",\"quick\",\"brown\",\"fox\",\"jumps\",\"over\",\"the\",\"Lazy\",\"dog\"};\n",
    "\n",
    "//显示数组的值\n",
    "System.Console.WriteLine(\"最初字符串数组包含以下值:\");\n",
    "PrintIndexAndValues(myArr);\n",
    "\n",
    "\n",
    "//调整数组小大更大(大5个元素)\n",
    "Array.Resize(ref myArr,myArr.Length+5); //改变数组大小\n",
    "\n",
    "//显示数组的值\n",
    "System.Console.WriteLine(\"调整后更大的尺寸,\");\n",
    "System.Console.WriteLine(\"字符串数组包含下列值:\");\n",
    "PrintIndexAndValues(myArr);\n",
    "\n",
    "//调整小组规模变小(4个元素)\n",
    "Array.Resize(ref myArr,4);\n",
    "\n",
    "//显示数组的值\n",
    "System.Console.WriteLine(\"调整后更小的尺寸,\");\n",
    "System.Console.WriteLine(\"字符串数组包含下列值:\");\n",
    "PrintIndexAndValues(myArr);\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "public static void PrintIndexAndValues(string[] myArr)\n",
    "{\n",
    "     System.Console.WriteLine($\"{\"索引\",-7}\\t{\"值\",-10}\");\n",
    "    System.Console.WriteLine($\"{\"----\",-7}\\t{\"-------\",-10}\");\n",
    "    for (int i = 0; i < myArr.Length; i++)\n",
    "    {\n",
    "    System.Console.WriteLine($\"{i+\":\",-7}\\t{myArr[i],-10}\");\n",
    "    }\n",
    "    System.Console.WriteLine();\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "* 从数组变量访问的实例成员\n",
    "    - 猎取特定维的长度不是用Length属性,而是用数组的GetLength()实例方法,调用时需指定返回哪一维的长度,如代码清单3.29所示  \n",
    "    - 数组全新拷贝需要使用数据的Clone()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.29\n",
    "bool[,,] cells;\n",
    "cells=new bool[2,3,3];\n",
    "System.Console.WriteLine(cells.GetLength(1)); //第1维的长度\n",
    "System.Console.WriteLine(cells.Rank);//数组的维数\n",
    "\n",
    "string[] myArr={\"The\",\"quick\",\"brown\",\"fox\",\"jumps\",\"over\",\"the\",\"Lazy\",\"dog\"};\n",
    "string[] Mycopy=(string[])myArr.Clone();\n",
    "string[] Myindex=myArr;\n",
    "Myindex[0]=\"the~\";\n",
    "System.Array.Resize(ref Mycopy,Mycopy.Length+4);\n",
    "PrintIndexAndValues(Mycopy);\n",
    "PrintIndexAndValues(myArr);\n",
    "PrintIndexAndValues(Myindex);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.30 \n",
    "string[] args;\n",
    "if (args[0][0]=='-')\n",
    "{\n",
    "    \n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "上述代码第一个数组访问符[]获取字符串数组args的第一个元素,第二个数组访问符则获取该字符串的条一个字符,等价于3.31"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "string[] args;\n",
    "string arg=args[0];\n",
    "if (arg[0]=='-')\n",
    "{\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "//3.32 反转字符串\n",
    "string reverse,palindrome;\n",
    "char[]temp;\n",
    "palindrome=\"O 2 O\";\n",
    "reverse =palindrome.Replace(\" \",\"\"); //把空格替换掉\n",
    "reverse=reverse.ToLower();//转换为小写\n",
    "temp=reverse.ToCharArray();//转换为字符数组\n",
    "System.Array.Reverse(temp);//反转\n",
    "if (reverse==new string(temp))  //将字符数组创建为字符串\n",
    "{\n",
    "    System.Console.WriteLine($\"\\\"{palindrome}\\\" 是回文.\");\n",
    "}else\n",
    "{\n",
    "    System.Console.WriteLine($\"\\\"{palindrome}\\\" 不是回文.\");\n",
    "}\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [
       "c#",
       "C#"
      ],
      "languageName": "C#",
      "name": "csharp"
     },
     {
      "aliases": [],
      "languageName": null,
      "name": ".NET"
     },
     {
      "aliases": [
       "f#",
       "F#"
      ],
      "languageName": "F#",
      "name": "fsharp"
     },
     {
      "aliases": [],
      "languageName": "HTML",
      "name": "html"
     },
     {
      "aliases": [],
      "languageName": "KQL",
      "name": "kql"
     },
     {
      "aliases": [],
      "languageName": "Mermaid",
      "name": "mermaid"
     },
     {
      "aliases": [
       "powershell"
      ],
      "languageName": "PowerShell",
      "name": "pwsh"
     },
     {
      "aliases": [],
      "languageName": "SQL",
      "name": "sql"
     },
     {
      "aliases": [],
      "name": "value"
     },
     {
      "aliases": [
       "frontend"
      ],
      "languageName": null,
      "name": "vscode"
     },
     {
      "aliases": [
       "js"
      ],
      "languageName": "JavaScript",
      "name": "javascript"
     },
     {
      "aliases": [],
      "name": "webview"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
